package com.pdstudio.carrecom.ui.activity.buy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.bean.Boutique;
import com.pdstudio.carrecom.bean.NewActivity;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.logger.Logger;
import com.pdstudio.carrecom.tools.JsonUtil;
import com.pdstudio.carrecom.ui.view.InfiniteIndicatorLayout;
import com.pdstudio.carrecom.ui.view.PageInfo;
import com.pdstudio.carrecom.ui.view.slideview.BaseSliderView;
import com.pdstudio.carrecom.ui.view.slideview.BaseSliderView.OnSliderClickListener;
import com.pdstudio.carrecom.ui.view.slideview.DefaultSliderView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 类: ActivityBoutiqueDetail <br>
 * 描述: 我要买车——精品展示——详情 <br>
 * 作者: ws <br>
 * 时间: 2015-6-15 下午11:32:03
 */
public class ActivityBoutiqueDetail extends Activity implements
		OnClickListener, OnSliderClickListener {

	private static final String TAG = "ActivityBoutiqueDetail";

	private static final int MSG_WHAT_ABOUT = 1;

	private TextView txtTitle;
	private ImageView ivBack;

	private ResultInfo mResultInfo;

	private AppContext mAppContext = AppContext.getInstance();

	private TextView mCategory;
	private TextView mPrice;
	private Boutique mBoutique;
	
	private boolean isCollect;

	private List<NewActivity> mDatas = new ArrayList<>();

	private ArrayList<PageInfo> viewInfos = new ArrayList<>();
	private InfiniteIndicatorLayout mAnimCircleIndicator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_boutique_detail);
		initTitle();
		initView();
		initData();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mAnimCircleIndicator.startAutoScroll();
		getBoutiques();
	}

	@Override
	public void onPause() {
		super.onPause();
		mAnimCircleIndicator.stopAutoScroll();
	}

	/**
	 * 
	 * 方法: 轮播动画 <br>
	 * 描述: TODO <br>
	 * 作者: ws <br>
	 * 时间: 2015-7-3 下午11:59:07
	 * 
	 * @param view
	 */
	private void animCircleIndicator() {

		for (PageInfo name : viewInfos) {
			DefaultSliderView textSliderView = new DefaultSliderView(this);
			textSliderView.image(name.getUrl())
					.setScaleType(BaseSliderView.ScaleType.Fit)
					.setOnSliderClickListener(this);
			textSliderView.getBundle().putString("extra", name.getData());
			mAnimCircleIndicator.addSlider(textSliderView);
		}
		mAnimCircleIndicator
				.setIndicatorPosition(InfiniteIndicatorLayout.IndicatorPosition.Center_Bottom);
	}

	/**
	 * 
	 * 方法: initImage <br>
	 * 描述: 轮播绑定url <br>
	 * 作者: ws <br>
	 * 时间: 2015-7-4 上午12:15:55
	 */
	private void initSliderImage() {

		viewInfos.clear();
		for (int i = 0; i < mDatas.size(); i++) {
			if (mDatas.get(i).url != null) {
				viewInfos.add(new PageInfo(mDatas.get(i).content,
						mDatas.get(i).url));
			}
		}

		animCircleIndicator();

	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.i(TAG, msg);
			switch (msg.what) {
			case MSG_WHAT_ABOUT:// 关于
				initSliderImage();
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("商家名称");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(this);
	}

	private void initView() {
		mCategory = (TextView) findViewById(R.id.boutique_category);
		mPrice = (TextView) findViewById(R.id.boutique_price);

		mAnimCircleIndicator = (InfiniteIndicatorLayout) findViewById(R.id.infinite_anim_circle);
	}

	private void initData() {
		Intent intent = getIntent();
		mBoutique = (Boutique) intent.getSerializableExtra("BOUTIQUE_DETAIL");
		isCollect = mBoutique.collect;

		mCategory.setText(mBoutique.getName());
		mPrice.setText(mBoutique.getPrice());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		}
	}

	/**
	 * 
	 * 方法: getBoutiques <br>
	 * 描述: 获取精品列表 <br>
	 * 作者: ws <br>
	 * 时间: 2015-6-16 上午12:27:40
	 */
	private void getBoutiques() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mNewCarBoutiqueListener);
			String url = ServiceHelper.BuyNewCarBoutiqueDetail;
			Map<String, Object> rps = new HashMap<String, Object>();
			rps.put("os", "android");
			rps.put("size", "30-60");
			rps.put("uid", mAppContext.getUserId());
			rps.put("boutiqueId", mBoutique.id);
			http.get(url, rps);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mNewCarBoutiqueListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			Logger.json(result);
			ResultInfo resultInfo = null;
			try {
				resultInfo = new Gson().fromJson(result,
						new TypeToken<ResultInfo>() {
						}.getType());
				if ((resultInfo != null)
						&& (resultInfo.msg
								.equals(ServiceHelper.msg_code_sucess))) {
					try {
						mResultInfo = resultInfo;

						mDatas = JsonUtil.fromJsonArray(
								JsonUtil.toJson(mResultInfo.data),
								NewActivity.class);
						Message msg = new Message();
						msg.what = MSG_WHAT_ABOUT;
						mHandler.sendMessage(msg);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int error, String msg) {

		}

		@Override
		public void onCancel() {

		}
	};

	@Override
	public void onSliderClick(BaseSliderView slider) {
		// TODO Auto-generated method stub

	}

}
